import pyOsi

theOSI = pyOsi.getOsi("CLP")  
if not theOSI:
	exit(-1)

"""
This section adapted from Matt Galati's example 
on the COIN-OR Tutorial website.

Problem from Bertsimas, Tsitsiklis page 21
 
 optimal solution: x* = (1,1)
 
 minimize -1 x0 - 1 x1
 s.t       1 x0 + 2 x1 <= 3
           2 x0 + 1 x1 <= 3
             x0        >= 0
             x1        >= 0
"""

n_cols = 2
objective = pyOsi.Double_array(n_cols)
col_lb = pyOsi.Double_array(n_cols)
col_ub = pyOsi.Double_array(n_cols)
n_rows = 2
row_lb = pyOsi.Double_array(n_rows)
row_ub = pyOsi.Double_array(n_rows)
start = pyOsi.Int_array(3)
index = pyOsi.Int_array(4)
value = pyOsi.Double_array(4)

objective[0] = -1.0 
objective[1] = -1.0
col_lb[0] = 0.0
col_lb[1] = 0.0
col_ub[0] = theOSI.getInfinity()
col_ub[1] = theOSI.getInfinity()
row_lb[0] = -1.0 * theOSI.getInfinity()
row_lb[1] = -1.0 * theOSI.getInfinity()
row_ub[0] = 3.0
row_ub[1] = 3.0

start[0] = 0
start[1] = 2
start[2] = 4
index[0] = 0
index[1] = 1
index[2] = 0
index[3] = 1
value[0] = 1.0
value[1] = 2.0
value[2] = 2.0
value[3] = 1.0


#load the problem to OSI
#theOSI.loadProblem(*matrix, col_lb, col_ub, objective, row_lb, row_ub);
#theOSI.loadProblem(*matrix, col_lb, col_ub, objective, row_lb, row_ub);
theOSI.loadProblem (n_cols,n_rows,start, index, value,
                    col_lb,col_ub,objective,row_lb,row_ub)

#Solve the (relaxation of the) problem
theOSI.initialSolve()

#Check the solution
if ( theOSI.isProvenOptimal() ):
      print "Found optimal solution!" 
      print "Objective value is %f" % theOSI.getObjValue()
      n = theOSI.getNumCols()
      solution = theOSI.getColSolution()
      for i in solution:
            print i
      
else: 
      print "Didn't find optimal solution."
